/**
 * 跨域
 */
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});


/**
 * 验证码
 * @param id
 */
function captchaInput(id) {
    if ($.cookie('captcha_' + id)) {
        var count = $.cookie('captcha_' + id);
        var btn = $('#code_de_' + id);
        btn.text(count + '秒后可获取').attr('disabled', true).css('cursor', 'not-allowed');
        var resend = setInterval(function () {
            count--;
            if (count > 0) {
                $('#code_' + id).attr('readOnly', false);
                $('#code_de_' + id).css("pointer-events", "none");
                btn.text(count + '秒后可获取').attr('disabled', true).css('cursor', 'not-allowed');
                $.cookie('captcha_' + id, count, {path: '/', expires: (1 / 86400) * count});
            } else {
                clearInterval(resend);
                $('#code_' + id).css("pointer-events", "block");
                btn.text("重新发送").removeClass('disabled').removeAttr('disabled style');
            }
        }, 1000);
        $("#email_code_" + id).attr('style', 'display:block');
    } else {
        $('#code_' + id).attr('readOnly', true);
    }
}

/*仿刷新：检测是否存在cookie*/
function captcha(uid, id, url, token) {
    var username = email(id);
    if (!username) {
        layer.msg('请填写正确邮箱号', {icon: 5, time: 3000});
        return false;
    }
    $.ajax({
        type: 'POST',
        url: url,
        data: {id: id, uid: uid, username: username, '_token': token},
        dataType: "json",
        beforeSend: function () {
            layer.load(1, {
                shade: [0.1, '#333333'] //0.1透明度的白色背景
            });
        },
        complete: function (XHR) {
            XHR = null;
        },
        error: function (xhr) {//获取ajax的错误信息
            layer.alert(xhr.responseText, "Failed",function () {
                layer.closeAll();
            });
        },
        success: function (response) {
            layer.closeAll();
            if (response.code == 403) {
                layer.msg(response.msg, {icon: 4, time: 3000})
                return false;
            }
            if (response.code != 200) {
                layer.msg(response.msg, {icon: 5, time: 3000});
                return false;
            }
            layer.msg(response.msg, {icon: 1, time: 5000});
            $("#email_code_" + id).attr('style', 'display:block');
            var box = $('#code_de_' + id);
            var unt = 60;
            var res = setInterval(function () {
                unt--;
                if (unt > 0) {
                    box.css("pointer-events", "none");
                    box.text(unt + '秒后可获取').attr('disabled', true).css('cursor', 'not-allowed');
                    $.cookie('captcha_' + id, unt, {path: '/', expires: (1 / 86400) * unt});
                } else {
                    clearInterval(res);
                    box.css("pointer-events", "block");
                    box.text("重新发送").removeAttr('disabled style');
                }
            }, 1000);
            box.attr('disabled', true).css('cursor', 'not-allowed');
            $('#code_' + id).attr('readOnly', false);
        }
    });
}

/**
 * 验证邮箱
 * @param id
 * @returns {boolean|jQuery}
 */
function email(id) {
    var username = $('#email_' + id + '').val();
    if (!(/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(username))) {
        return false;
    }

    return username;
}

//非手机端，设置高度
if (window.innerWidth >= 768) {
    $('.email-content').css('height', window.innerHeight - 30);
}
$(window).resize(function () {
    $('.email-content').css('height', window.innerHeight - 30);
})

/**
 * 验证全数字
 * @param num
 * @returns {boolean}
 */
function num(num) {
    return (/^[0-9]+$/g).test(num);
}

/**
 * 每三位数加，
 * @param str
 * @returns {string|string}
 */
function formatNum(str) {
    var name = false;
    if (str[0] == '-') {
        str = replaceNum(str);
        name = true;
    } else {
        name = false;
    }
    var newStr = '';
    var count = 0;
    for (var i = str.length - 1; i >= 0; i--) {
        if (count % 3 == 0 && count != 0) {
            newStr = str.charAt(i) + ',' + newStr;
        } else {
            newStr = str.charAt(i) + newStr;
        }
        count++;
    }
    if (name) {
        return '-' + newStr;
    } else {
        return newStr;
    }
}

/**
 * 去除所有非数字 和数字的第一个0
 * @param str
 * @returns {*}
 */
function replaceNum(str) {
    str = str.replace(/[^0-9]/ig, "");
    return str.replace(/\b(0+)/gi, "");
}

/**
 * 正則验证+-×÷
 * @param str
 * @returns {string|boolean}
 */
function passNum(str) {
    if (str.match(/\+/gi)) {
        return '+';
    } else if (str.match(/\-/gi)) {
        return '-';
    } else if (str.match(/\×/gi)) {
        return '×';
    } else if (str.match(/\÷/gi)) {
        return '÷';
    } else {
        return false;
    }
}

